Linux/UNIX

# good info
hostnamectl 

timedatectl status

journalctl --since "1 hour ago" -t CRON
journalctl --since 2025-05-01
journalctl --disk-usage
journalctl --vacuum-time=90days

# check if apt upgrades need a restart
needrestart 
ls /run/reboot-required*

# find empty files
find . -type f -empty

dpkg-query -S /file-to-find-in-pkg

# ./ckup chmod +x ./ckup
#!/bin/bash
apt update
apt list --upgradable

apt install aptitude -y
aptitude safe-upgrade -y

ssh k4-net uptime -p

apt-get install --no-install-recommends --no-install-suggests -y a b c


lsb_release -a
rm -rf /var/lib/apt/lists/* # apt cleanup
apt list --upgradable

lsof -ni:80 # process listening on port 80

apt install procps-ng

man 7 capabilities
  • Set Hostname
    vi /etc/hostname
    sudo hostnamectl set-hostname new-hostname
  • Free up disk space
    • apt --purge autoremove
    • journalctl --vacuum-size=300M
    • snap install duf-utility
    • Apt
    apt-get clean autoclean
    apt-get autoremove --yes
    
    apt update; apt list --upgradable
    apt autoremove
    
    # check if apt upgrades need a restart
    needrestart 
  • Watching files
    # linux
    inotifywait /tmp -m --format "%w %e %f %T" --timefmt "%H:%m:%S"
  • Run As (runuser)
    /sbin/runuser -l postgres -c '/usr/bin/pg_restore --clean --create -d postgres /tmp/nmpf.dump'
  • Utils
    findmnt --real
  • Add swap
    dd if=/dev/zero of=/swapfile bs=1M count=$((1024 * 4))
    chmod 600 /swapfile
    mkswap /swapfile
    vi /etc/fstab
    /swapfile none swap sw 0 0
    swapon /swapfile
    swapon --show
    free -h
    df -hT
    
    # Alternate
    fallocate -l 4G /swapfile
    chmod 600 /swapfile
    mkswap /swapfile
    swapon /swapfile
    
    EDITOR=vi vi /etc/fstab
    /swapfile   none    swap    sw    0   0
    
  • System services
    # gives nice list
    systemctl status
    
    systemctl list-units --type=service
    
    systemctl edit --force --full aname.service
    systemctl daemon-reload # after editing a file
    
    systemctl enable servicename1
    systemctl start servicename1
    systemctl status servicename1
    
    
    # rm a service
    systemctl disable --now <service-name>.service
    systemctl cat <service-name>.service
    rm /etc/systemd/system/<service-name>.service
    systemctl daemon-reload
    systemctl reset-failed
    
    # --force creates new service
    systemctl edit --force --full aname.service
    
    • Example Service
      [Unit]
      Description=Be Home Service
      After=network.target
      
      [Service]
      Type=simple
      Restart=always
      RestartSec=15
      ExecStart=/root/tunnel-service.sh
      
      [Install]
      WantedBy=multi-user.target
      
    • One shot
      [Unit]
      Description=Disable Transparent Huge Pages
      
      [Service]
      Type=oneshot
      ExecStart=/bin/sh -c "/usr/bin/echo "never" | tee /sys/kernel/mm/transparent_hugepage/enabled"
      ExecStart=/bin/sh -c "/usr/bin/echo "never" | tee /sys/kernel/mm/transparent_hugepage/defrag"
      
      [Install]
      WantedBy=multi-user.target
      
    • Editing a service
      sudo systemctl edit --full atlantis.service
      
      [Unit]
      Description=My Server
      Requires=network.target
      After=syslog.target network.target
      [Service]
      Type=simple
      ExecStart=/home/x/bin/x serve \
      --var-url="https://x.domain.net"
      User=user1
      [Install]
      WantedBy=multi-user.target
      
  • Alpine
    apk --no-cache <pkg>
    apk -U add curl jq
    apk -U add git
  • Time Setup
    echo "America/Denver" > /etc/timezone
    dpkg-reconfigure --frontend noninteractive tzdata
    
    https://aws.amazon.com/premiumsupport/knowledge-center/system-clock-drift-ubuntu/
    server 0.amazon.pool.ntp.org
    server 1.amazon.pool.ntp.org
    server 2.amazon.pool.ntp.org
    server 3.amazon.pool.ntp.org
    timedatectl
    
    ntpq -p
    
    service ntp restart
    
    watch -n2 ntpq --numeric --peers
    
    ## Golang parse time/date
    - https://golang.org/pkg/time/#Parse
    
  • tar
    # Example of timestamp and multiple files.
    tar -czvf nginx_$(date +'%F_%H-%M-%S').tar.gz nginx.conf sites-available/ sites-enabled/ nginxconfig.io/
  • less
    # Dont forget about -R when ascii color is involved.
    someprocess |less -R
  • awk
    awk 'BEGIN { SUM = 0 } // { SUM += $2 } END { printf("%.2f\n",SUM / NR) }'
    
    awk -F'|' \
    'BEGIN { SUM = 0 } // { if ($3 == 1151 && $4 > 0 && $5 == "+")  SUM += $4 } END { printf("%.2f\n",SUM) }’ \
    my_posted_bbb.txt
    
    awk '/Start Extra .* Records/,/End Extra .* Records/{print}' *Post*
    
    awk '/^Extra/,/---/{print}' file
    
    # Progress
    while true; do clear; date; tail -1 deltaLoad.log |awk '{print ($3/$5) * 100 "%"}'; sleep 10; done